#include <string.h>
#include "protocol.h"

#if 0
int memcmp(void *pMem1, void *pMem2, int iLen)
{
	int i;
	char *pMemChar1, *pMemChar2;

	pMemChar1 = (char *)pMem1;
	pMemChar2 = (char *)pMem2;

	for (i = 0; i < iLen; i++)
	{
		if (pMemChar1[i] != pMemChar2[i])
		{
			return (1);
		}
	}

	return (0);
}

int memcpy(void *pDst, void *pSrc, int iLen)
{
	int i;
	char *pDstChar, *pSrcChar;

	pDstChar = (char *)pDst;
	pSrcChar = (char *)pSrc;

	for (i = 0; i < iLen; i++)
	{
		pDstChar[i] = pSrcChar[i];
	}

	return (0);
}

int ProtocolIsHeaderValid(unsigned char *pHeaderBuf)
{
	S_PROTOCOL_HEADER *pHeader = (S_PROTOCOL_HEADER *)pHeaderBuf;

	if (memcmp(pHeader->cMagicNum, PROTOCOL_MAGIC_NUM, sizeof(pHeader->cMagicNum)))
	{
		return (0);
	}

	if ((pHeader->eMsgNo < E_MSG_NO_MIN) || (pHeader->eMsgNo >= E_MSG_NO_MAX))
	{
		return (0);
	}

	return (1);
}
#endif

void ProtocolInitHeader(unsigned char *pMsgBuf, E_MSG_NO eMsgNo, int iMsgLen)
{
	S_PROTOCOL_HEADER *pHeader = (S_PROTOCOL_HEADER *)pMsgBuf;

	memcpy(pHeader->cMagicNum, PROTOCOL_MAGIC_NUM, sizeof(pHeader->cMagicNum));
	pHeader->iLen = iMsgLen;
	pHeader->eMsgNo = eMsgNo;
}
